package com.joy.common.interceptor;

import cn.hutool.core.util.StrUtil;
import com.google.common.collect.Maps;
import com.joy.common.constant.PageCons;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Method;
import java.util.Enumeration;
import java.util.Map;

/**
 * @ClassName: DrawInterceptor
 * @Description: 用于操作添加、修改 后返回页面，搜索条件不清空
 * @Author: yjzhao
 * @Create: 2019-09-19  14:07
 **/
@Slf4j
@Component
public class DrawInterceptor extends HandlerInterceptorAdapter {

    public static String DRAW_INDEX_KEY = "DRAW_INDEX_KEY";
    public static String DRAW_ATTRMAP_KEY = "DRAW_ATTRMAP_KEY";

    @Override
    public void afterCompletion(HttpServletRequest request,
                                HttpServletResponse response, Object handler, Exception ex)
            throws Exception {

    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response,
                           Object handler, ModelAndView mav) throws Exception {
        String requestURI = request.getRequestURI();

        if (StrUtil.isEmpty(requestURI) || "/".equals(requestURI)) {
            return;
        }
        Subject subject = SecurityUtils.getSubject();
        String userNm = (String)subject.getPrincipal();
        //如果用户是通过RememberMe登录的，判断一下有没有被锁定
        //第一次，记得写session 中的user_id
        Session session = subject.getSession();

        RequestMapping annotation = null;
        if (handler instanceof HandlerMethod) {
            HandlerMethod h = (HandlerMethod) handler;
            Method method = h.getMethod();
            String methodNm = method.getName();
            Object keyObj = session.getAttribute(DRAW_INDEX_KEY);
            Object attrmapObj = session.getAttribute(DRAW_ATTRMAP_KEY);
            if ("index".equals(methodNm)) {
                if (keyObj != null) {
                    String key = (String) keyObj;
                    if (requestURI.equals(key) && attrmapObj != null) {
                        mav.addAllObjects((Map<String, Object>) attrmapObj);
                    } else {
                        session.setAttribute(DRAW_INDEX_KEY, requestURI);
                    }
                } else {
                    session.setAttribute(DRAW_INDEX_KEY, requestURI);
                }
            } else if ("pagination".equals(methodNm)) {

                Enumeration<String> attributeNames = request.getParameterNames();

                Map<String, Object> paramMap = Maps.newHashMap();
                while (attributeNames.hasMoreElements()) {
                    String paramName = attributeNames.nextElement();
                    if(paramName.startsWith("columns[")) {
                        continue;
                    }
                    String attribute = request.getParameter(paramName);
                    if(attribute == null) {
                        continue;
                    }
                    if (PageCons.PAGE_START.equals(paramName)) {
                        paramMap.put("oldStart", attribute);
                    }
                    if (PageCons.PAGE_LENGTH.equals(paramName)) {
                        paramMap.put("oldLength", attribute);
                    }
                    paramName = "old_" + paramName;
                    paramMap.put(paramName, attribute);
                }
                session.setAttribute(DRAW_ATTRMAP_KEY, paramMap);
            }
        }

    }

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        return super.preHandle(request, response, handler);
    }
}
